﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;


public partial class Hospital_PurchaseGoods : System.Web.UI.Page
{

    string BuyPlanId, strSql, strConditions;
    public int AccountID_yy;
    public string projectid, color;

    protected void Page_Load(object sender, EventArgs e)
    {

        BuyPlanId = SysFun.IsSQL(SysFun.ToTrim(Request.QueryString["BuyPlanId"]));
        projectid = SysFun.RequestDecrypt(Request.QueryString["projectid"]);
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumHospitale);


        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            AccountID_yy = SysFun.ToInt(_Account.AccountId);
            LabHospital.Text = _Account.DWName;
            strConditions = fctConditions();

            switch (projectid)
            {
                case "0":  //  固定,默认为挂网价
                    color = "#000000";
                    break;
                case "1":  //  可修改,必须低于挂网价

                    color = "#990099";
                    break;
                case "2":  //  可修改,无限制

                    color = "#0033CC";
                    break;
                default:
                    color = "#000000";
                    break;
            }

            if (!IsPostBack)
            {

                Db db = new Db();
                System.Data.IDataReader dr;
                strSql = "select * from PurchasePlan where BuyPlanId=" + BuyPlanId + " and AccountId_YY=" + _Account.AccountId;
                dr = db.DataReader(strSql);
                if (dr.Read())
                {
                    txtPlanName.Text = SysFun.ToTrim(dr["PlanName"]);

                    if (SysFun.ToInt(dr["BuyStatus"]) == 2)
                    {
                        Response.Write("<script>alert('采购单已提交！不能浏览该页面。')</script>");
                        Response.Write("<script>self.close();</script>");
                    }
                }
                else
                {
                    Response.Write("对不起，你没有权限浏览不属于自身采购单的药品");
                    Response.End();
                }
                dr.Close();
                dr.Dispose();
                db.Close();

            }

            if (Request.Form["btnSubmit"] == "确定选取")
            {
                if (Request.Form["chk"] == null)
                {
                    Response.Write("<script>alert('请勾选药品！');window.history.go(-1);</script>");

                }
                else
                {
                    int rowCun = 0;
                    Db db = new Db();
                    string SupplyGoodsId = Request.Form["chk"].ToString();

                    strSql = "select count(SupplyGoodsId) as cun from BuyPlanGoods where  SupplyGoodsId in (" + SupplyGoodsId + ") and BuyplanId=" + BuyPlanId + " and AccountId_YY=" + AccountID_yy;
                    System.Data.IDataReader dr;

                    dr = db.DataReader(strSql);
                    if (dr.Read())
                    {
                        rowCun = SysFun.ToInt(dr["cun"]);
                    }
                    dr.Close();
                    dr.Dispose();

                    if (rowCun > 0)
                    {
                        Response.Write("<script language=JavaScript>alert('所选药品已经在该采购单中存在，不能添加！！！');</script>");
                    }
                    else
                    {
                        string[] SGID = SupplyGoodsId.Length == 0 ? new string[] { "" } : SupplyGoodsId.Substring(0).Split(new char[] { ',' });
                        strSql = "";
                        string _num, _strAccountId_PS, _strBidPrice;
                        for (int i = 0; i < SGID.Length; i++)
                        {
                            _strAccountId_PS = SysFun.ToTrim(Request.Form["AccountId_PS" + SGID[i]]);
                            _strBidPrice = SysFun.ToTrim(Request.Form["txtprice" + SGID[i]]);//交易价格
                            _num = SysFun.ToTrim(Request.Form["txtnum" + SGID[i]]);//交易数量
                            strSql += "@" + "Insert Into BuyPlanGoods(projectid,GoodsNum,SupplyGoodSId,BuyPlanId,AccountId_YY,AccountId_PS,dealStatus,TradePrice,purchaseprice) values(" +
                                projectid + "," + _num + "," + SGID[i].ToString() + "," + BuyPlanId + "," + AccountID_yy + "," + _strAccountId_PS + ",0," + _strBidPrice + "," + Convert.ToDecimal(_strBidPrice) * Convert.ToDecimal(_num) + ")";

                        }
                        if (strSql.Length > 0)
                        {
                            strSql += "@" + "update PurchasePlan set BuyStatus='1' where BuyPlanId=" + BuyPlanId;

                            db.ExecuteTransaction(strSql.Substring(1).Split(new char[] { '@' }), 30);
                        }
                        Response.Write("<script language=JavaScript>window.alert('选取成功!');</script>");
                    }
                    db.Close();

                }
            }


            showGridView();
        }
    }

    public void showGridView()
    {


        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "PurchaseTable";                                               //要查询的表明
        dbPage.Key = "HspId";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "HspId,projectid,GoodsId,ProductName,SupplyGoodSId,MedicineModel,IsNull(Unit,'')+'/'+IsNull(PackUnit,'') As Pack1,OutLookC,GoodsName,UserCode_SC,CompanyName_SC,CompanyName_TB,UserCode_PS,CompanyName_PS,PurchasePrice,AccountId_PS,factor,CASE QualityLevels " + ConfigurationManager.AppSettings["QualityLevels"].ToString() + " END AS QualityLevels,convert(numeric(20,3),bidprice) as bidprice ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "ProductName,CompanyName_PS";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();


        DlAttorney.DataSource = dr;
        DlAttorney.DataBind();

        dr.Close();
        dr.Dispose();

        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键


    }

    public string fctConditions()
    {
        string _strTemp = "";
        string nNumber = DropDownList1.SelectedValue;
        _strTemp = " AccountId_YY=" + AccountID_yy + " and State='正常采购'" + " and (CompanyName_ps is not null and CompanyName_ps<>'') and isuse=1 and";
        String _strProductName, _strGoodsId, _strUserCode_SC, _strCompanyName_SC, _strUserCode_PS, _strCompanyName_PS, _strGoodsName;

        //通用名
        _strProductName = TextProductName.Text.ToString().Trim();
        if (_strProductName != "")
        {
            _strTemp += "  (ProductName like '%" + _strProductName + "%' or Product_SpellingCode like '%" + _strProductName + "%') and";
        }

        //商品名
        _strGoodsName = txtGoodsName.Text.ToString().Trim();
        if (_strGoodsName != "")
        {
            _strTemp += " GoodsName like '%" + _strGoodsName + "%' and ";
        }

        //药品流水号
        _strGoodsId = txtGoodsId.Text.ToString().Trim();
        if (_strGoodsId != "")
        {
            _strTemp += " GoodsId = '" + _strGoodsId + "' and";
        }
        //生产企业名称
        _strCompanyName_SC = TextCompanyName_SC.Text.ToString().Trim();
        if (_strCompanyName_SC != "")
        {
            _strTemp += " CompanyName_SC like '%" + _strCompanyName_SC + "%' and";
        }
        //生产企业编号
        _strUserCode_SC = Textuscoder_sc.Text.ToString().Trim();
        if (_strUserCode_SC != "")
        {
            _strTemp += " UserCode_SC like '%" + _strUserCode_SC + "%' and";
        }
        //配送企业名称

        _strCompanyName_PS = TextCompanyName_PS.Text.ToString().Trim();
        if (_strCompanyName_PS != "")
        {
            _strTemp += " CompanyName_PS like '%" + _strCompanyName_PS + "%' and";
        }
        //配送企业编号

        _strUserCode_PS = Textuscoder_ps.Text.ToString().Trim();
        if (_strUserCode_PS != "")
        {
            _strTemp += " UserCode_PS like '%" + _strUserCode_PS + "%' and";
        }
        //
        if (nNumber != "2") //显示未选的药品
        {
            _strTemp += " SupplyGoodsId not in (select SupplyGoodsId from BuyPlanGoods where BuyplanId=" + BuyPlanId + " and AccountId_YY=" + AccountID_yy + ") and";
        }

        if (_strTemp != "")
        {
            _strTemp = "  " + _strTemp.Substring(0, _strTemp.Length - 3);
        }
        return _strTemp;

        //TextCompanyName_PS.Items.Add(new ListItem("安徽"));

        //TextCompanyName_SC.Items.Add(new ListItem("1"));
        //TextCompanyName_SC.Items.Add(new ListItem(""));
        //TextCompanyName_SC.Items.Add(new ListItem("2"));
    }


    protected void DlAttorney_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


}
